{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bb345c8e-7cdf-4944-8392-c60816afecc6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T03:33:16.603567Z",
     "iopub.status.busy": "2022-03-27T03:33:16.603259Z",
     "iopub.status.idle": "2022-03-27T03:33:16.834124Z",
     "shell.execute_reply": "2022-03-27T03:33:16.833253Z",
     "shell.execute_reply.started": "2022-03-27T03:33:16.603493Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47f58b47-64ec-4ed9-8e44-5ee3c7663ce1",
   "metadata": {},
   "source": [
    "1. 不拷贝\n",
    "    * 直接赋值，没有拷贝，仅在stack同区域里为数组定义了不同的名称\n",
    "2. 浅拷贝\n",
    "    * 只在stack里开辟了新区域进行拷贝，仍指向原heap空间中的数据\n",
    "3. 深拷贝\n",
    "    * 在stack和heap里都开辟里的新区域进行拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7759d9df-ff4a-4f57-ba02-7a4c96bed9f6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T03:43:13.002707Z",
     "iopub.status.busy": "2022-03-27T03:43:13.002458Z",
     "iopub.status.idle": "2022-03-27T03:43:13.008875Z",
     "shell.execute_reply": "2022-03-27T03:43:13.007396Z",
     "shell.execute_reply.started": "2022-03-27T03:43:13.002683Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "# 1. 不拷贝\n",
    "# 创建以为数组a1，元素为0-9之间的整数\n",
    "a1 = np.arange(10)\n",
    "# 把a1直接赋值给a2\n",
    "a2 = a1\n",
    "# 查看a2与a1是不是同一个数组对象\n",
    "print(a2 is a1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "53b60581-c443-49d8-9bf5-b093aba0ef51",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T03:45:05.965163Z",
     "iopub.status.busy": "2022-03-27T03:45:05.964908Z",
     "iopub.status.idle": "2022-03-27T03:45:05.970037Z",
     "shell.execute_reply": "2022-03-27T03:45:05.969203Z",
     "shell.execute_reply.started": "2022-03-27T03:45:05.965134Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "[100   1   2   3   4   5   6   7   8   9]\n"
     ]
    }
   ],
   "source": [
    "# 2. 浅拷贝\n",
    "# 把a1浅拷贝给a3\n",
    "a3 = a1.view()\n",
    "# 查看a3与a1是不是同一个数组对象\n",
    "print(a3 is a1)\n",
    "# 修改a3第0个位置的元素值为100\n",
    "a3[0] = 100\n",
    "# 查看a1的变化\n",
    "print(a1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "697cc685-4c46-4518-82e6-5d70327578ba",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T03:46:47.244952Z",
     "iopub.status.busy": "2022-03-27T03:46:47.244692Z",
     "iopub.status.idle": "2022-03-27T03:46:47.250219Z",
     "shell.execute_reply": "2022-03-27T03:46:47.248968Z",
     "shell.execute_reply.started": "2022-03-27T03:46:47.244926Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[100 200   2   3   4   5   6   7   8   9]\n",
      "[100   1   2   3   4   5   6   7   8   9]\n"
     ]
    }
   ],
   "source": [
    "# 3. 深拷贝\n",
    "# 把a1深拷贝给a4\n",
    "a4 = a1.copy()\n",
    "# 修改a4第1个位置的元素值为200\n",
    "a4[1] = 200\n",
    "# 查看a4的变化\n",
    "print(a4)\n",
    "# 查看a1的变化\n",
    "print(a1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
